package com.rocksea.app.fragment;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.rocksea.app.BaseFragment;
import com.rocksea.app.R;
import com.rocksea.app.activity.BaseShowActivity;
import com.rocksea.app.activity.StaticLoadActivity;
import com.rocksea.app.db.DbCenter;
import com.rocksea.app.db.StaticLoad.StaticLoad;
import com.rocksea.app.weight.StaticLoadCurveView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import butterknife.InjectView;

/**
 * @author Administrator
 * @date 2017/11/30
 */

public class StaticLoadSlgQCurveFrament extends BaseFragment
{
    @InjectView(R.id.BootView)
    LinearLayout mLinearLayout;

    public static StaticLoadSlgQCurveFrament newInstance(Bundle args) {

        StaticLoadSlgQCurveFrament fragment = new StaticLoadSlgQCurveFrament();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {
        String baseInfoId = getArguments().getString(BaseShowActivity.BASEINFOID_KEY);
        int code = getArguments().getInt(StaticLoadActivity.TYPE_KEY);
        loading();
        AsyQueryTask asyQueryTask = new AsyQueryTask(baseInfoId,code);
        asyQueryTask.execute();
    }

    @Override
    protected void requestAgain() {

    }

    @Override
    protected int getContentViewId() {
        return R.layout.slgq_layout;
    }


    public String makeJyDocJSON(StaticLoad staticLoad) throws JSONException {
        JSONArray staticLoadJsonArray = new JSONArray();
        for (int i = 0; i < staticLoad.mStaticLoadTest.size(); i++)
        {
            //转JSON
            JSONObject sample = new JSONObject();
            StaticLoad.StaticLoadTest loadTest = staticLoad.mStaticLoadTest.get(i);

            sample.put("Grade",loadTest.grade);
            sample.put("SampleCount",loadTest.sampleCount);
            sample.put("SampleTime",loadTest.sampleTime);
            sample.put("Loading",loadTest.loading);
            sample.put("LoadDirect",loadTest.loadDirect);
            sample.put("TimeCount",loadTest.timeCount);
            sample.put("RealLoading",loadTest.realLoading);
            sample.put("RealPress",loadTest.realPress);
            sample.put("SAverage",loadTest.saverage);
            sample.put("Id",0);
            for (int n = 0; n < loadTest.sensorValue.length; n++)
            {
                if (loadTest.sensorValue[n]<80&&loadTest.sensorValue[n]>-80)
                    sample.put(String.format("S%d",n+1),loadTest.sensorValue[n]);
                else
                    sample.put(String.format("S%d",n+1),"null");
            }
            staticLoadJsonArray.put(sample);
        }
        return staticLoadJsonArray.toString();

    }


    private class AsyQueryTask extends AsyncTask<Void,Void,StaticLoad>
    {
        private String baseInfoId;
        private int code;
        public AsyQueryTask(String baseInfoId, int code)
        {
            this.baseInfoId = baseInfoId;
            this.code = code;
        }

        @Override
        protected void onPreExecute()
        {
            super.onPreExecute();
        }

        @Override
        protected StaticLoad doInBackground(Void... params)
        {
            return DbCenter.mStaticLoadDbHelper.queryById(baseInfoId);
        }
        @Override
        protected void onPostExecute(StaticLoad staticLoad)
        {
            super.onPostExecute(staticLoad);
            if (staticLoad==null)
                loadFailed();
            else
            {
                loadCompleted();
                try
                {
                    StaticLoadCurveView.StaticLoadCurveFormat format = new StaticLoadCurveView.StaticLoadCurveFormat(
                            makeJyDocJSON(staticLoad),
                            staticLoad.mStaticLoadTest.size(),
                            (float) staticLoad.boardArea,
                            staticLoad.standard);
                    StaticLoadCurveView staticLoadCurveView = new StaticLoadCurveView(getContext(),format,StaticLoadCurveView.S_LGQ);
                    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                    staticLoadCurveView.setLayoutParams(lp);
                    mLinearLayout.addView(staticLoadCurveView);
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
        }
    }
}
